<?php

class NoteSet
{
	private $notes;
	
	private $firstId;
	private $lastId;
	
	// Constructor
	function __construct($notes = false)
	{
		$this->notes = array();
		
		if(is_array($notes))
			$this->setNotes($notes);
	}
	
	##########################
	# Appends (= adds at the end) an instance of Note
	# Use : append($note)
	# Param : note object
	# Returns :
	public function append($note)
	{
		$this->notes[] = $note;
		
		// Update last id
		$this->setLastId($note->getId());
		
		// Update first id is this note is the first to be added
		if($this->count() == 1)
			$this->setFirstId($note->getId());
	}
	
	##########################
	# Prepends (= adds at the beginning) an instance of Note
	# Use : prepend($note)
	# Param : note object
	# Returns :
	public function prepend($note) 
	{
		array_unshift($note,$this->notes);
		
		// Update first id
		$this->setFirstId($note->getId());
		
		// Update last id is this note is the first to be added
		if($this->count() == 1)
			$this->setLastId($note->getId());
	}
	
	##########################
	# Gets the range of id of the notes
	# Use : $array = $noteset->getRange(); $first_id = $array[0]; $last_id = $array[1];
	# Param :
	# Returns : array
	public function getRange()
	{
		$range = array();
		
		if($this->getLastId() > $this->getFirstId())
		{
			$range[0] = $this->getFirstId();
			$range[1] = $this->getLastId();
		}
		else
		{
			$range[0] = $this->getLastId();
			$range[1] = $this->getFirstId();
		}
		
		return $range;
	}
	
	##########################
	# Gets the id of the FIRST note, whatever the sorting is (ASC or DESC)
	# Use : $first_id = $noteset->getAbsoluteFirstId();
	# Param :
	# Returns : int
	public function getAbsoluteFirstId()
	{
		$range = $this->getRange();
		return $range[0];
	}
	
	##########################
	# Gets the id of the LAST note, whatever the sorting is (ASC or DESC)
	# Use : $last_id = $noteset->getAbsoluteLastId();
	# Param :
	# Returns : int
	public function getAbsoluteLastId()
	{
		$range = $this->getRange();
		return $range[1];
	}
	
	
	##########################
	# Setters
	
	public function setNotes($notes)
	{
		if(!is_array($notes))
		{
			throw new Exception('Argument must be an array of Note instances');
			return;
		}

		$this->notes = $notes;
				
		// Set first and last id
		$this->setFirstId($this->notes[0]->getId());
		$this->setLastId($this->notes[$this->count()-1]->getId());
	}
	
	public function setLastId($id)
	{
		if(is_numeric($id))
			return $this->lastId = $id;
		else
			return false;
	}
	
	public function setFirstId($id)
	{
		if(is_numeric($id))
			return $this->firstId = $id;
		else
			return false;
	}
	
	##########################
	# Getters
	
	public function getNotes()
	{
		return $this->notes;
	}
	
	public function getFirstId()
	{
		return $this->firstId;
	}
	
	public function getLastId()
	{
		return $this->lastId;
	}
	
	public function count()
	{
		return count($this->notes);
	}
	
}

?>